/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.l2device.gameserver.model.events.tvt;

import java.util.Calendar;
import java.util.HashMap;
import java.util.logging.Logger;

import com.l2device.gameserver.Config;
import com.l2device.gameserver.ThreadPoolManager;

/**
 * @author Anarchy
 *
 * Just a class that will handle the registering and loading of events.
 */
public final class EventManager
{
       private static final Logger _log = Logger.getLogger(EventManager.class.getName());
      
       public static int TVT = 1;
      
       private int _events = 0;
       public HashMap<IEvent, Integer> _eventsTasks;
       private IEvent _currentEvent = null;
      
       public static EventManager getInstance()
       {
               return SingletonHolder._instance;
       }
      
       protected EventManager()
       {
               _eventsTasks = new HashMap<>();
              
               if (Config.ALLOW_TVT_EVENT)
               		registerEvent(Config.TVT_EVENT_TIMES, TvTEvent.class);
               _log.info("Loaded " + _events + " events.");
       }
      
       public void registerEvent(final String times, final Class<?> type)
       {
               _events++;
              
               int i = 0;
              
               for (String time : times.split(","))
               {
                       final IEvent event;
                      
                       try
                       {
                               event = (IEvent)type.newInstance();
                       }
                       catch (InstantiationException | IllegalAccessException ea)
                       {
                               ea.printStackTrace();
                               return;
                       }
                      
                       String[] timeArray = time.split(":");
                       int hours = Integer.parseInt(timeArray[0]);
                       int minutes = Integer.parseInt(timeArray[1]);
                      
                       Calendar cld = Calendar.getInstance();
                      
                       cld.set(Calendar.HOUR_OF_DAY, hours);
                       cld.set(Calendar.MINUTE, minutes);
                       cld.set(Calendar.SECOND, 0);
                      
                       if (cld.getTimeInMillis() - System.currentTimeMillis() < 0)
                       {
                               cld.set(Calendar.DAY_OF_MONTH, cld.get(Calendar.DAY_OF_MONTH) + 1);
                               cld.set(Calendar.HOUR_OF_DAY, hours);
                               cld.set(Calendar.MINUTE, minutes);
                               cld.set(Calendar.SECOND, 0);
                       }
                      
                       if (cld.getTimeInMillis() - System.currentTimeMillis() > 0)
                       {
                               i++;
                              
                               if (_eventsTasks.containsKey(event))
                               {
                                       _eventsTasks.remove(event);
                                       _eventsTasks.put(event, i);
                               }
                               else
                               {
                                       _eventsTasks.put(event, i);
                               }
                              
                               ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
                               {
                                       @Override
                                       public void run()
                                       {
                                               if (_eventsTasks.get(event) == 1)
                                               {
                                                       _eventsTasks.remove(event);
                                                       registerEvent(times, type);
                                               }
                                               else
                                               {
                                                       int t = _eventsTasks.get(event);
                                                       _eventsTasks.remove(event);
                                                       _eventsTasks.put(event, t--);
                                               }
                                              
                                               event.runEvent();
                                       }
                               }, cld.getTimeInMillis() - System.currentTimeMillis());
                       }
               }
       }
      
       public IEvent getCurrentEvent()
       {
               return _currentEvent;
       }
      
       public void setCurrentEvent(IEvent event)
       {
               _currentEvent = event;
       }
      
       private static class SingletonHolder
       {
               protected static final EventManager _instance = new EventManager();
       }
}